﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace ORMTool.Framework
{
    public class BoolToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool && targetType == typeof(Visibility))
            {
                if ((bool)value)
                    return (object)Visibility.Visible;
                return parameter != null && parameter is Visibility ? parameter : (object)Visibility.Collapsed;
            }
            if (value != null)
                return (object)Visibility.Visible;
            return parameter != null && parameter is Visibility ? parameter : (object)Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is Visibility) || !(targetType == typeof(bool)))
                throw new ArgumentException("Invalid argument/return type. Expected argument: Visibility and return type: bool");
            return (Visibility)value == Visibility.Visible ? (object)true : (object)false;
        }
    }
    public class NullableValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return DependencyProperty.UnsetValue;
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
